Spring Boot-এ RestTemplate
এবং WebClient
এর জন্য Unit Testing করতে হলে আপনাকে Mocking টুল (যেমন: Mockito) ব্যবহার করতে হবে। নিচে RestTemplate
এবং WebClient
-এর জন্য Unit Testing-এর বিস্তারিত উদাহরণ দেওয়া হলো।
JUnit এবং Mockito ব্যবহার করুন।
Maven Dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String fetchData() {
String url = "https://api.example.com/data";
return restTemplate.getForObject(url, String.class);
}
}
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.web.client.RestTemplate;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
public class ApiServiceTest {
@Mock
private RestTemplate restTemplate;
@InjectMocks
private ApiService apiService;
@Test
public void testFetchData() {
// Mock response
String mockResponse = "Mocked API Response";
String url = "https://api.example.com/data";
// Define behavior for mock RestTemplate
when(restTemplate.getForObject(url, String.class)).thenReturn(mockResponse);
// Call the method
String result = apiService.fetchData();
// Assertions
assertEquals(mockResponse, result);
verify(restTemplate, times(1)).getForObject(url, String.class);
}
}
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
@Service
public class ApiService {
private final WebClient webClient;
public ApiService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("https://api.example.com").build();
}
public Mono<String> fetchData() {
return webClient.get()
.uri("/data")
.retrieve()
.bodyToMono(String.class);
}
}
WebClient-এর জন্য
WebClient.Builder` Mock করা হয়।
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.ClientResponse;
import reactor.core.publisher.Mono;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
public class ApiServiceTest {
@Mock
private WebClient.Builder webClientBuilder;
@Mock
private WebClient webClient;
@Mock
private WebClient.RequestHeadersUriSpec<?> requestHeadersUriSpec;
@Mock
private WebClient.ResponseSpec responseSpec;
@InjectMocks
private ApiService apiService;
@Test
public void testFetchData() {
// Mock API Response
String mockResponse = "Mocked API Response";
// Mock WebClient behavior
when(webClientBuilder.baseUrl("https://api.example.com")).thenReturn(webClientBuilder);
when(webClientBuilder.build()).thenReturn(webClient);
when(webClient.get()).thenReturn(requestHeadersUriSpec);
when(requestHeadersUriSpec.uri("/data")).thenReturn(requestHeadersUriSpec);
when(requestHeadersUriSpec.retrieve()).thenReturn(responseSpec);
when(responseSpec.bodyToMono(String.class)).thenReturn(Mono.just(mockResponse));
// Call the method
String result = apiService.fetchData().block();
// Assertions
assertEquals(mockResponse, result);
verify(webClient, times(1)).get();
verify(requestHeadersUriSpec, times(1)).uri("/data");
}
}
Spring Boot ক্লায়েন্ট API ইন্টিগ্রেশন টেস্টের জন্য MockMVC ব্যবহার করতে পারেন।
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
public class ApiControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testGetData() throws Exception {
mockMvc.perform(get("/data"))
.andExpect(status().isOk());
}
}
Mockito
ব্যবহার করে getForObject
, postForObject
ইত্যাদি মক করা হয়।WebClient.Builder
এবং সংশ্লিষ্ট মেথডগুলো Mock করতে হয়।এভাবে আপনি RestTemplate
এবং WebClient
এর জন্য Unit Testing করতে পারেন।
Read more